<!--
 * @Author: liaoxing
 * @Date: 2021-09-01 11:20:01
 * @LastEditors: liaoxing
 * @LastEditTime: 2021-09-01 14:03:09
 * @Description: file content
 * @FilePath: \数据中心\src\views\home\BoardPage\DataBoard\charts\barCharts2.vue
-->
<!--
 * @Author: liaoxing
 * @Date: 2021-08-16 15:21:00
 * @LastEditors: liaoxing
 * @LastEditTime: 2021-09-01 09:57:37
 * @Description: file content
 * @FilePath: \数据中心\src\views\home\BoardPage\DataBoard\charts\barCharts.vue
-->
<template>
  <div :id="chatId" class="areaLineCharts"></div>
</template>

<script>
const echarts = require('echarts')
export default {
  name: 'annularchart',
  props: {
    id: {
      type: String,
    },
    dataInfo: {
      type: Object,
      default: () => {
        return {
          sameMonth: [3000,2000,3000,4000,3000,2000,1000,4000,6000,1600,3000,4000],
          compare: [6000,5000,4000,7000,5000,4000,3000,6000,8000,3600,5000,6000]
        }
      }
    },
  },
  data() {
    return {
      chatId: '',
      option: null,
      myChart: null,
      fontSize: 12,
    }
  },
  created() {
    console.log(this.id)
    this.chatId = this.id
  },
  watch: {
    dataInfo: {
      deep: true,
      handler() {
        this.getOption(0)
      },
    },
  },
  mounted() {
    let that = this
    this.$nextTick(() => {
      that.drawChat()
    })
  },
  activated() {
    // 只有设置了keep-alive才生效
    this.$nextTick(() => {
      if (document.getElementById(this.chatId).clientHeight === 0) {
        console.log('activated...')
        this.myChart.resize()
      }
    })
  },
  methods: {
    drawChat() {
      var chartDom = document.getElementById(this.chatId)
      this.myChart = echarts.init(chartDom, null, { renderer: 'svg' })
      this.getOption(1)
    },
    getOption(type) {
      this.option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none'
          },
          borderWidth: 0,
          borderColor: '#104db1',
          backgroundColor: '#104db1',
          textStyle: {
            color: '#fff'
          }
        },
        legend: {
            data: ['销售', '订单'],
            icon: 'rect',
            itemWidth: 14,
            textStyle: {
                color: '#fff'
            },
            orient: 'vertical',
            right: 0,
            top: 0
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: {
          type: 'category',
          data: ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
              color: '#fff'
          }
        },
        yAxis: {
          type: 'value',
          splitLine: {
            show: false,
          },
          name: '单位：百万',
          nameGap: 25,
          axisLabel: {
              color: '#fff'
          },
          nameTextStyle: {
              color: '#fff',
              padding: [0,60,0,0]
          }
        },
        series: [
          {
            name: '销售',
            type: 'bar',
            emphasis: {
              focus: 'series',
            },
            itemStyle: {
              borderRadius: [20, 20, 0, 0],
              color: '#38befb',
            },
            data: this.dataInfo.sameMonth,
            label: {
                show: true,
                position: 'inside',
                rotate: 90,
                color: '#fff',
                fontSize: 10
            }
          },
          {
            name: '订单',
            type: 'bar',
            emphasis: {
              focus: 'series',
            },
            itemStyle: {
              borderRadius: [20, 20, 0, 0],
              color: '#ff690b',
            },
            data: this.dataInfo.compare,
            label: {
                show: true,
                position: 'inside',
                rotate: 90,
                color: '#fff',
                fontSize: 10
            }
          }
        ],
      }
      this.myChart.setOption(this.option, true)
      if (!type) return
      window.addEventListener('resize', this.listenResize)
    },
    listenResize() {
      let resizeTimer = setTimeout(() => {
        clearTimeout(resizeTimer)
        this.fontSize = (window.fontSize || 100) * 0.12
        this.getOption(0)
        this.myChart.resize()
      }, 100)
    },
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.listenResize)
    if (!this.myChart) return
    this.myChart.clear()
  },
  deactivated() {
    window.removeEventListener('resize', this.listenResize)
    if (!this.myChart) return
    this.myChart.clear()
  },
}
</script>

<style>
.areaLineCharts {
  width: 100%;
  height: 100%;
  /* border: 1px solid wheat; */
}
</style>
